package com.rayo.accesscontrollercopy.model.network;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rayo.accesscontrollercopy.config.GlobalVariables;
import com.rayo.accesscontrollercopy.config.OfficeModeConfig;
import com.rayo.accesscontrollercopy.model.callback.ApiCallback;
import com.rayo.accesscontrollercopy.model.db.bean.BlackListData;
import com.rayo.accesscontrollercopy.model.db.bean.CalendarData;
import com.rayo.accesscontrollercopy.model.db.bean.CardTimezoneData;
import com.rayo.accesscontrollercopy.model.db.bean.CardUserData;
import com.rayo.accesscontrollercopy.model.db.bean.DSTData;
import com.rayo.accesscontrollercopy.model.db.bean.HeartData;
import com.rayo.accesscontrollercopy.model.db.bean.KeyTimezoneData;
import com.rayo.accesscontrollercopy.model.db.bean.KeyUserData;
import com.rayo.accesscontrollercopy.model.db.bean.OperatorData;
import com.rayo.accesscontrollercopy.model.db.bean.PlanKeyAddData;
import com.rayo.accesscontrollercopy.model.db.bean.PlanKeyDelData;
import com.rayo.accesscontrollercopy.model.db.bean.PlanKeyTempData;
import com.rayo.accesscontrollercopy.model.db.bean.SiteData;
import com.rayo.accesscontrollercopy.model.db.dao.BlackListDao;
import com.rayo.accesscontrollercopy.model.db.dao.BlackListImpl;
import com.rayo.accesscontrollercopy.model.db.dao.CalendarDao;
import com.rayo.accesscontrollercopy.model.db.dao.CalendarImpl;
import com.rayo.accesscontrollercopy.model.db.dao.CardTimeDao;
import com.rayo.accesscontrollercopy.model.db.dao.CardTimeImpl;
import com.rayo.accesscontrollercopy.model.db.dao.CardUserDao;
import com.rayo.accesscontrollercopy.model.db.dao.CardUserImpl;
import com.rayo.accesscontrollercopy.model.db.dao.DSTDao;
import com.rayo.accesscontrollercopy.model.db.dao.DSTImpl;
import com.rayo.accesscontrollercopy.model.db.dao.DmuTimeDao;
import com.rayo.accesscontrollercopy.model.db.dao.DmuTimeImpl;
import com.rayo.accesscontrollercopy.model.db.dao.HeartDao;
import com.rayo.accesscontrollercopy.model.db.dao.HeartImpl;
import com.rayo.accesscontrollercopy.model.db.dao.KeyTimeDao;
import com.rayo.accesscontrollercopy.model.db.dao.KeyTimeImpl;
import com.rayo.accesscontrollercopy.model.db.dao.KeyUserDao;
import com.rayo.accesscontrollercopy.model.db.dao.KeyUserImpl;
import com.rayo.accesscontrollercopy.model.db.dao.OperatorDao;
import com.rayo.accesscontrollercopy.model.db.dao.OperatorImpl;
import com.rayo.accesscontrollercopy.model.db.dao.PlanKeyAddDao;
import com.rayo.accesscontrollercopy.model.db.dao.PlanKeyAddImpl;
import com.rayo.accesscontrollercopy.model.db.dao.PlanKeyDelDao;
import com.rayo.accesscontrollercopy.model.db.dao.PlanKeyDelImpl;
import com.rayo.accesscontrollercopy.model.db.dao.PlanKeyTempDao;
import com.rayo.accesscontrollercopy.model.db.dao.PlanKeyTempImpl;
import com.rayo.accesscontrollercopy.model.db.dao.SiteDao;
import com.rayo.accesscontrollercopy.model.db.dao.SiteImpl;
import com.rayo.accesscontrollercopy.model.network.bean.BaseApi;
import com.rayo.accesscontrollercopy.model.network.bean.HeartApi;
import com.rayo.accesscontrollercopy.model.network.bean.LockIdApi;
import com.rayo.accesscontrollercopy.model.network.bean.OpenApi;
import com.rayo.accesscontrollercopy.model.network.bean.UpdateCalenderApi;
import com.rayo.accesscontrollercopy.model.network.bean.UpdateCardTimezonApi;
import com.rayo.accesscontrollercopy.model.network.bean.UpdateKeyTimezoneApi;
import com.rayo.accesscontrollercopy.model.network.bean.UpdateLockApi;
import com.rayo.accesscontrollercopy.model.network.bean.UpdateOperatorApi;
import com.rayo.accesscontrollercopy.model.network.bean.UpdateTimeApi;
import com.rayo.accesscontrollercopy.model.network.bean.UpdateUserApi;
import com.rayo.wallreader.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by yanghaojie on 2017/2/14.
 */

public class AnalysisData {
    private BaseApi mBaseApi;
    private ApiCallback mApiCallback;
    private JSONObject mJSONObject;
    private int keyCount, keyPos;
    private int cardCount, cardPos;
    private HeartDao mHeartDao = new HeartImpl();
    private HeartData mHeartData = new HeartData();

    public void analysis(String json, ApiCallback apiCallback) {
        mApiCallback = apiCallback;
        mBaseApi = new BaseApi();
        try {
            mJSONObject = JSON.parseObject(json);
            mBaseApi.setCmd(mJSONObject.getInteger(BaseApi.CMD));
            mBaseApi.setSite(mJSONObject.getString(BaseApi.SITE));
            mBaseApi.setData(mJSONObject.get(BaseApi.DATA));
        } catch (Exception e) {
            e.printStackTrace();
            mBaseApi = null;
            mJSONObject = null;
        }
        if (null == mBaseApi || mJSONObject == null) {
            if (null != mApiCallback) {
                mApiCallback.onFailure();
            }
            return;
        }
        if (serverData(mBaseApi, mJSONObject)) return;

        if (null != mApiCallback) {
            if (mBaseApi.getCmd() < 0) {
                mApiCallback.onError((byte) -1, mJSONObject.getString(BaseApi.DATA));
            } else {
                mApiCallback.onSuccess((byte) mBaseApi.getCmd(), mJSONObject.getJSONObject(BaseApi.DATA));
            }
        }
    }

    private boolean serverData(BaseApi baseApi, JSONObject jsonObject) {
        switch (baseApi.getCmd()) {
            case ApiCmd.OPEN:
                OpenApi openApi = JSON.parseObject(jsonObject.getString(BaseApi.DATA), OpenApi.class);
                if (GlobalVariables.getInstance().getmSiteData().getLockid() == Integer.valueOf(openApi.getLockid())) {
                    sendBack(ApiCmd.OPEN, "");
                } else {
                    sendBack(ApiCmd.ERROR, "lock id is wrong");
                }
                break;
            case ApiCmd.UPDATE_LOCK:
                if (GlobalVariables.getInstance().getmSiteData().getSite().isEmpty())
                    GlobalVariables.getInstance().getmSiteData().setSite(mBaseApi.getSite());
                LockIdApi lockIdApi = JSON.parseObject(jsonObject.getString(BaseApi.DATA), LockIdApi.class);
//                WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(new BaseApi(ApiCmd.UPDATE_LOCK,
//                        mBaseApi.getSite(), new Object())), mApiCallback);
                HeartApi heartApi = new HeartApi();
                heartApi.setLockid(lockIdApi.getLockid());
                mHeartData = mHeartDao.findById(mBaseApi.getSite(), Integer.valueOf(lockIdApi.getLockid()));
                if (null == mHeartData) {
                    mHeartData = new HeartData();
                    mHeartData.setSite(mBaseApi.getSite());
                    mHeartData.setLockid(lockIdApi.getLockid());
                    mHeartData.updateDate(TimeUtils.dateFromNotYMDHMS("010102010100"));
                }
                heartApi.setKeyuser(mHeartData.getKeyusertime());
                heartApi.setCarduser(mHeartData.getCardusertime());
                WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(new BaseApi(ApiCmd.HEART,
                        mBaseApi.getSite(), heartApi)));
                break;
            case ApiCmd.HEART:
                HeartData heartData = JSON.parseObject(jsonObject.getString(BaseApi.DATA), HeartData.class);
                mHeartData = mHeartDao.findById(mBaseApi.getSite(), Integer.valueOf(heartData.getLockid()));
                if (null == mHeartData) {
                    mHeartData = new HeartData();
                    mHeartData.setSite(mBaseApi.getSite());
                    mHeartData.setLockid(heartData.getLockid());
                    mHeartData.updateDate(TimeUtils.dateFromNotYMDHMS("010102010100"));
                }
                if (heartData.getStatus() == 0 || heartData.getStatus() == 1) {
                    WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(
                            new BaseApi(ApiCmd.UPDATE_ID, mBaseApi.getSite(),
                                    new LockIdApi(mHeartData.getLockid()))));
                } else {
                    if (null != mApiCallback)
                        mApiCallback.onSuccess(ApiCmd.UPDATE_ID, null);
                }
                if (heartData.getOperator().getTime() > mHeartData.getOperator().getTime()) {
                    WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(
                            new BaseApi(ApiCmd.UPDATE_OPERATOR, mBaseApi.getSite(),
                                    new UpdateTimeApi(mHeartData.getOperator()))));
                } else {
                    if (null != mApiCallback)
                        mApiCallback.onSuccess(ApiCmd.UPDATE_OPERATOR, null);
                }
                if (heartData.getTimezone().getTime() > mHeartData.getTimezone().getTime()) {
                    WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(
                            new BaseApi(ApiCmd.UPDATE_USER_TIMEZONE, mBaseApi.getSite(),
                                    new UpdateTimeApi(mHeartData.getTimezone()))));
                } else {
                    if (null != mApiCallback)
                        mApiCallback.onSuccess(ApiCmd.UPDATE_USER_TIMEZONE, null);
                }
                if (heartData.getCalendar().getTime() > mHeartData.getCalendar().getTime()) {
                    WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(
                            new BaseApi(ApiCmd.UPDATE_CALENDER, mBaseApi.getSite(),
                                    new UpdateTimeApi(mHeartData.getCalendar()))));
                } else {
                    if (null != mApiCallback)
                        mApiCallback.onSuccess(ApiCmd.UPDATE_CALENDER, null);
                }
                if (heartData.getCardtime().getTime() > mHeartData.getCardtime().getTime()) {
                    WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(
                            new BaseApi(ApiCmd.UPDATE_CARD_TIMEZONE, mBaseApi.getSite(),
                                    new UpdateTimeApi(mHeartData.getCardtime()))));
                } else {
                    if (null != mApiCallback)
                        mApiCallback.onSuccess(ApiCmd.UPDATE_CARD_TIMEZONE, null);
                }
                if (heartData.getBlacklist().getTime() > mHeartData.getBlacklist().getTime()) {
                    WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(
                            new BaseApi(ApiCmd.UPDATE_BLACKLIST, mBaseApi.getSite(),
                                    new UpdateTimeApi(mHeartData.getCardtime()))));
                } else {
                    if (null != mApiCallback)
                        mApiCallback.onSuccess(ApiCmd.UPDATE_BLACKLIST, null);
                }
                if (heartData.getKeylist().size() > 0) {
                    keyCount = heartData.getCardlist().size();
                    keyPos = 0;
                    KeyUserDao keyUserDao = new KeyUserImpl();
                    for (String keyId : heartData.getKeylist()) {
                        KeyUserData keyUserData = keyUserDao.findByUserid(keyId);
                        UpdateUserApi updateUserApi = new UpdateUserApi(keyId, new Date());
                        if (keyUserData.getUserid().isEmpty() || keyUserData.getUserid().equals("0")) {
                            updateUserApi.setTime(TimeUtils.dateFromNotYMDHMS("010102010100"));
                        } else {
                            updateUserApi.setTime(keyUserData.getEditime());
                        }
                        WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(
                                new BaseApi(ApiCmd.UPDATE_KEY, mBaseApi.getSite(), updateUserApi)));
                    }
                } else {
                    if (null != mApiCallback)
                        mApiCallback.onSuccess(ApiCmd.UPDATE_KEY, null);
                }
                if (heartData.getCardlist().size() > 0) {
                    cardCount = heartData.getCardlist().size();
                    cardPos = 0;
                    CardUserDao cardUserDao = new CardUserImpl();
                    for (String keyId : heartData.getCardlist()) {
                        CardUserData cardUserData = cardUserDao.findByUserId(Integer.valueOf(keyId));
                        UpdateUserApi updateUserApi = new UpdateUserApi(keyId, new Date());
                        if (cardUserData.getUserid() == 0) {
                            updateUserApi.setTime(TimeUtils.dateFromNotYMDHMS("010102010100"));
                        } else {
                            updateUserApi.setTime(cardUserData.getEditime());
                        }
                        WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(
                                new BaseApi(ApiCmd.UPDATE_CARD, mBaseApi.getSite(), updateUserApi)));
                    }
                } else {
                    if (null != mApiCallback)
                        mApiCallback.onSuccess(ApiCmd.UPDATE_CARD, null);
                }
                WebSocketHelp.getmInstance().setAppVersion(heartData.getAppversion());
                break;
            case ApiCmd.UPDATE_ID:
                UpdateLockApi updateLockApi = JSON.parseObject(jsonObject.getString(BaseApi.DATA), UpdateLockApi.class);
                SiteDao siteDao = new SiteImpl();
                SiteData siteData = siteDao.findFirst();
                if (null == siteData) siteData = new SiteData();
                siteData.setSite(updateLockApi.getSite());
                siteData.setLockid(Integer.valueOf(updateLockApi.getLockid()));
                siteData.setGroupid(Integer.valueOf(updateLockApi.getGroupid()));
                siteData.setName(updateLockApi.getName());
                siteData.setMode(updateLockApi.getMode());
                siteData.setRegcode(updateLockApi.getRegcode());
                siteData.setSyscode(updateLockApi.getSyscode());
                if (siteData.getId() == 0)
                    siteDao.addSite(siteData);
                else
                    siteDao.updateSite(siteData);
                mHeartData.setSite(updateLockApi.getSite());
                mHeartData.setLockid(updateLockApi.getLockid());
                mHeartDao.addEvent(mHeartData);
                GlobalVariables.getInstance().setmSiteData(siteData);
                DmuTimeDao dmuTimeDao = new DmuTimeImpl();
                dmuTimeDao.deleteAll();
                dmuTimeDao.addAll(updateLockApi.getTimezone());
                OfficeModeConfig.getInstance().setDmuTimeDatas(updateLockApi.getTimezone());
                if (null != mApiCallback)
                    mApiCallback.onSuccess(ApiCmd.UPDATE_ID, null);
                sendBack(ApiCmd.UPDATE_LOCK, "");
                break;
            case ApiCmd.UPDATE_BLACKLIST:
                BlackListData blackListData = JSON.parseObject(
                        jsonObject.getString(BaseApi.DATA), BlackListData.class);
                if (null != blackListData) {
                    BlackListDao blackListDao = new BlackListImpl();
                    blackListDao.updateBlackList(blackListData);
                    mHeartData.setBlacklist(new Date());
                    mHeartDao.addEvent(mHeartData);
                }
                if (null != mApiCallback)
                    mApiCallback.onSuccess(ApiCmd.UPDATE_BLACKLIST, null);
                break;
            case ApiCmd.UPDATE_OPERATOR:
                UpdateOperatorApi updateOperatorApi = JSON.parseObject(
                        jsonObject.getString(BaseApi.DATA), UpdateOperatorApi.class);
                if (null != updateOperatorApi && updateOperatorApi.getList().size() > 0) {
                    OperatorDao operatorDao = new OperatorImpl();
                    for (OperatorData operatorData : updateOperatorApi.getList()) {
                        OperatorData operatorData1 = operatorDao.findByOperid(operatorData.getOperid());
                        if (null == operatorData1) operatorData1 = new OperatorData();
                        operatorData1.setFirst(operatorData.getFirst());
                        operatorData1.setLast(operatorData.getLast());
                        operatorData1.setOperid(operatorData.getOperid());
                        operatorData1.setLogin(operatorData.getLogin());
                        operatorData1.setPassword(operatorData.getPassword());
                        operatorData1.setAuthority(operatorData.getAuthority());
                        if (operatorData1.getId() == 0) operatorDao.addOperator(operatorData1);
                        else operatorDao.updateOperator(operatorData1);
                    }
                    mHeartData.setOperator(new Date());
                    mHeartDao.addEvent(mHeartData);
                }
                if (null != mApiCallback)
                    mApiCallback.onSuccess(ApiCmd.UPDATE_OPERATOR, null);
                break;
            case ApiCmd.UPDATE_USER_TIMEZONE:
                UpdateKeyTimezoneApi updateKeyTimezoneApi = JSON.parseObject(
                        jsonObject.getString(BaseApi.DATA), UpdateKeyTimezoneApi.class);
                if (null != updateKeyTimezoneApi && updateKeyTimezoneApi.getList().size() > 0) {
                    KeyTimeDao keyTimeDao = new KeyTimeImpl();
                    for (KeyTimezoneData keyTimezoneData : updateKeyTimezoneApi.getList()) {
                        KeyTimezoneData keyTimezoneData1 = keyTimeDao.findByNo(keyTimezoneData.getNo(), keyTimezoneData.getSerial());
                        if (keyTimezoneData1 ==null ) {
                            keyTimezoneData1 = new KeyTimezoneData();
                            keyTimezoneData1.setNo(keyTimezoneData.getNo());
                            keyTimezoneData1.setSerial(keyTimezoneData.getSerial());
                            keyTimezoneData1.setFrom(keyTimezoneData.getFrom());
                            keyTimezoneData1.setTo(keyTimezoneData.getTo());
                            keyTimezoneData1.setMon(keyTimezoneData.getMon());
                            keyTimezoneData1.setTue(keyTimezoneData.getTue());
                            keyTimezoneData1.setWed(keyTimezoneData.getWed());
                            keyTimezoneData1.setThu(keyTimezoneData.getThu());
                            keyTimezoneData1.setFri(keyTimezoneData.getFri());
                            keyTimezoneData1.setSat(keyTimezoneData.getSat());
                            keyTimezoneData1.setSun(keyTimezoneData.getSun());
                            keyTimezoneData1.setSp1(keyTimezoneData.getSp1());
                            keyTimezoneData1.setSp2(keyTimezoneData.getSp2());
                            keyTimezoneData1.setHol(keyTimezoneData.getHol());
                            keyTimeDao.addKeyTime(keyTimezoneData1);
                        } else {
                            keyTimezoneData1.setNo(keyTimezoneData.getNo());
                            keyTimezoneData1.setSerial(keyTimezoneData.getSerial());
                            keyTimezoneData1.setFrom(keyTimezoneData.getFrom());
                            keyTimezoneData1.setTo(keyTimezoneData.getTo());
                            keyTimezoneData1.setMon(keyTimezoneData.getMon());
                            keyTimezoneData1.setTue(keyTimezoneData.getTue());
                            keyTimezoneData1.setWed(keyTimezoneData.getWed());
                            keyTimezoneData1.setThu(keyTimezoneData.getThu());
                            keyTimezoneData1.setFri(keyTimezoneData.getFri());
                            keyTimezoneData1.setSat(keyTimezoneData.getSat());
                            keyTimezoneData1.setSun(keyTimezoneData.getSun());
                            keyTimezoneData1.setSp1(keyTimezoneData.getSp1());
                            keyTimezoneData1.setSp2(keyTimezoneData.getSp2());
                            keyTimezoneData1.setHol(keyTimezoneData.getHol());
                            keyTimeDao.updateKeyTime(keyTimezoneData1);
                        }
                    }
                    mHeartData.setTimezone(new Date());
                    mHeartDao.addEvent(mHeartData);
                }
                if (null != mApiCallback)
                    mApiCallback.onSuccess(ApiCmd.UPDATE_USER_TIMEZONE, null);
                break;
            case ApiCmd.UPDATE_CARD_TIMEZONE:
                UpdateCardTimezonApi updateCardTimezonApi = JSON.parseObject(
                        jsonObject.getString(BaseApi.DATA), UpdateCardTimezonApi.class);
                if (null != updateCardTimezonApi && updateCardTimezonApi.getList().size() > 0) {
                    CardTimeDao cardTimeDao = new CardTimeImpl();
                    for (CardTimezoneData cardTimezoneData : updateCardTimezonApi.getList()) {
                        CardTimezoneData cardTimezoneData1 = cardTimeDao.findByNo(cardTimezoneData.getUserid(), cardTimezoneData.getSerial());
                        if (null == cardTimezoneData1) cardTimezoneData1 = new CardTimezoneData();
                        cardTimezoneData1.setUserid(cardTimezoneData.getUserid());
                        cardTimezoneData1.setSerial(cardTimezoneData.getSerial());
                        cardTimezoneData1.setFrom(cardTimezoneData.getFrom());
                        cardTimezoneData1.setTo(cardTimezoneData.getTo());
                        cardTimezoneData1.setMon(cardTimezoneData.getMon());
                        cardTimezoneData1.setTue(cardTimezoneData.getTue());
                        cardTimezoneData1.setWed(cardTimezoneData.getWed());
                        cardTimezoneData1.setThu(cardTimezoneData.getThu());
                        cardTimezoneData1.setFri(cardTimezoneData.getFri());
                        cardTimezoneData1.setSat(cardTimezoneData.getSat());
                        cardTimezoneData1.setSun(cardTimezoneData.getSun());
                    }
                    mHeartData.setCardtime(new Date());
                    mHeartDao.addEvent(mHeartData);
                }
                if (null != mApiCallback)
                    mApiCallback.onSuccess(ApiCmd.UPDATE_CARD_TIMEZONE, null);
                break;
            case ApiCmd.UPDATE_CALENDER:
                UpdateCalenderApi updateCalenderApi = JSON.parseObject(
                        jsonObject.getString(BaseApi.DATA), UpdateCalenderApi.class);
                if (null != updateCalenderApi && updateCalenderApi.getList().size() > 0) {
                    CalendarDao calendarDao = new CalendarImpl();
                    for (UpdateCalenderApi.CalenderData calenderData : updateCalenderApi.getList()) {
                        int calid = Integer.valueOf(calenderData.getCalid());
                        for (CalendarData calendarData : calenderData.getDatelist()) {
                            calendarData.setCalid(calid);
                        }
                        calendarDao.deleteByCalId(calid);
                        calendarDao.addAll(calenderData.getDatelist());
                    }
                    mHeartData.setCalendar(new Date());
                    mHeartDao.addEvent(mHeartData);
                }
                if (null != mApiCallback)
                    mApiCallback.onSuccess(ApiCmd.UPDATE_CALENDER, null);
                break;
            case ApiCmd.UPDATE_DST:
                DSTData dstData = JSON.parseObject(
                        jsonObject.getString(BaseApi.DATA), DSTData.class);
                if (null != dstData) {
                    DSTDao dstDao = new DSTImpl();
                    dstDao.update(dstData);
                    mHeartData.setDst(new Date());
                    mHeartDao.addEvent(mHeartData);
                }
                if (null != mApiCallback)
                    mApiCallback.onSuccess(ApiCmd.UPDATE_DST, null);
                break;
            case ApiCmd.UPDATE_KEY:
                KeyUserData keyUserData = JSON.parseObject(
                        jsonObject.getString(BaseApi.DATA), KeyUserData.class);
                keyPos++;
                if (null != keyUserData) {
                    KeyUserDao keyUserDao = new KeyUserImpl();
                    PlanKeyDelDao planKeyDelDao = new PlanKeyDelImpl();
                    PlanKeyAddDao planKeyAddDao = new PlanKeyAddImpl();
                    PlanKeyTempDao planKeyTempDao = new PlanKeyTempImpl();
                    if (keyUserData.getFlag() == 0) {
                        keyUserDao.addKeyUser(keyUserData);
                        if (keyUserData.getPlan().length() > 2) {
                            List<PlanKeyAddData> keyAddDataList = new ArrayList<>();
                            String keyAddData = keyUserData.getPlan().substring(1, keyUserData.getPlan().length() - 1);

                            while (128 < keyAddData.length()) {
                                keyAddDataList.add(new PlanKeyAddData(Integer.valueOf(keyUserData.getUserid()),
                                        keyAddData.substring(0, 128)));
                                keyAddData = keyAddData.substring(128, keyAddData.length());
                            }
                            keyAddDataList.add(new PlanKeyAddData(Integer.valueOf(keyUserData.getUserid()),
                                    keyAddData));

                            planKeyAddDao.addOrupdateKeyPlan(Integer.valueOf(keyUserData.getUserid()), keyAddDataList);
                        }
                        if (keyUserData.getDeleteplan().length() > 2) {
                            List<PlanKeyDelData> keyDelDataList = new ArrayList<>();
                            String keyDelData = keyUserData.getPlan().substring(1, keyUserData.getDeleteplan().length() - 1);

                            while (128 < keyDelData.length()) {
                                keyDelDataList.add(new PlanKeyDelData(Integer.valueOf(keyUserData.getUserid()),
                                        keyDelData.substring(0, 128)));
                                keyDelData = keyDelData.substring(128, keyDelData.length());
                            }
                            keyDelDataList.add(new PlanKeyDelData(Integer.valueOf(keyUserData.getUserid()),
                                    keyDelData));
                            planKeyDelDao.addOrupdateKeyPlan(Integer.valueOf(keyUserData.getUserid()), keyDelDataList);
                        }
                        if (keyUserData.getTemplan().size() > 0) {
                            for (PlanKeyTempData planKeyTempData : keyUserData.getTemplan()) {
                                planKeyTempData.setUserid(Integer.valueOf(keyUserData.getUserid()));
                                planKeyTempDao.addOrUpdateKeyPlan(planKeyTempData);
                            }
                        }
                    } else {
                        keyUserDao.deleteByKeyUserId(Integer.valueOf(keyUserData.getUserid()));
                        planKeyAddDao.deleteByUserId(Integer.valueOf(keyUserData.getUserid()));
                        planKeyDelDao.deleteByUserId(Integer.valueOf(keyUserData.getUserid()));
                        planKeyTempDao.deletTempKeyPlanById(Integer.valueOf(keyUserData.getUserid()));
                    }
                }
                if (keyPos >= keyCount) {
                    if (keyCount > 0) {
                        mHeartData.setKeyusertime(new Date());
                        mHeartDao.addEvent(mHeartData);
                    }
                    keyPos = 0;
                    keyCount = 0;
//                    if (null != mApiCallback)
//                        mApiCallback.onSuccess(ApiCmd.UPDATE_KEY, keyUserData);
                }
                break;
            case ApiCmd.UPDATE_CARD:
                CardUserData cardUserData = JSON.parseObject(
                        jsonObject.getString(BaseApi.DATA), CardUserData.class);
                cardPos++;
                if (null != cardUserData) {
                    CardUserDao cardUserDao = new CardUserImpl();
                    cardUserDao.addCardUser(cardUserData);
                }
                if (cardPos >= cardCount) {
                    if (cardCount > 0) {
                        mHeartData.setCardusertime(new Date());
                        mHeartDao.addEvent(mHeartData);
                    }
                    cardPos = 0;
                    cardCount = 0;
//                    if (null != mApiCallback)
//                        mApiCallback.onSuccess(ApiCmd.UPDATE_CARD, cardUserData);
                }
                break;
            default:
                return false;
        }
        return true;
    }

    private void sendBack(int cmd, String data) {
        BaseApi baseApi = new BaseApi(cmd,
                GlobalVariables.getInstance().getmSiteData().getSite(), new Object());
        if (!data.isEmpty()) baseApi.setData(data);

        WebSocketHelp.getmInstance().sendMessage(JSON.toJSONString(baseApi));
    }
}
